<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Anwsion dev team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/


if (!defined('IN_ANWSION'))
{
	die;
}

class special extends AWS_CONTROLLER
{
	function get_permission_action()
	{
	
	}

	public function setup()
	{
		$this->model('admin_session')->init($this->get_permission_action());

		TPL::assign('menu_list', $this->model('admin_group')->get_menu_list($this->user_info['group_id'], 304));
	}

	public function index_action()
	{
		$this->list_action();
	}

	public function list_action()
	{
		$per_page = 20;
		
		$page_id = intval($_GET['page_id']);

		$page_id = ($page_id == 0) ? 1 : $page_id;
		
		$limit = ($page_id - 1) * $per_page . "," . $per_page;
		
		$feature_list = $this->model('special')->get_special_list1(null, false, 'id DESC', $limit);
		
		$feature_count = $this->model('special')->get_special_list1(null, true);
		
		AWS_APP::pagination()->initialize(array(
			'base_url' => get_setting('base_url') . '/?/admin/special/list/',
			'total_rows' => $feature_count,
			'per_page' => $per_page,
			'last_link' => "末页",
			'first_link' => "首页",
			'next_link' => "下一页 »",
			'prev_link' => "« 上一页",
			'anchor_class' => ' class="number"',
			'cur_tag_open' => '<a class="number current">',
			'cur_tag_close' => '</a>',
			'direct_page' => TRUE
		));
		
		$this->crumb("专题管理", "admin/special/list/");
		
		TPL::assign('pagination', AWS_APP::pagination()->create_links());
		
		TPL::assign('list', $feature_list);
		
		TPL::output("admin/special/list");
	}
	public function site_list_action()
	{
		$per_page = 20;
		$special_id = intval($_GET['special_id']);
		
		$page_id = intval($_GET['page_id']);
	
		$page_id = ($page_id == 0) ? 1 : $page_id;
	
		$limit = ($page_id - 1) * $per_page . "," . $per_page;
	
		$feature_list = $this->model('special')->get_specialSite_list($special_id, false, 'id DESC', $limit);
		
		$feature_count = $this->model('special')->get_specialSite_list('  special_id='.$special_id, true);
	//	print_r($feature_list);
		AWS_APP::pagination()->initialize(array(
		'base_url' => get_setting('base_url') . '/?/admin/special/list/',
		'total_rows' => $feature_count,
		'per_page' => $per_page,
		'last_link' => "末页",
		'first_link' => "首页",
		'next_link' => "下一页 »",
		'prev_link' => "« 上一页",
		'anchor_class' => ' class="number"',
		'cur_tag_open' => '<a class="number current">',
		'cur_tag_close' => '</a>',
		'direct_page' => TRUE
		));
	
		$this->crumb("专题管理", "admin/special/list/");

		// 评分插件
		TPL::import_js('js/jraty/js/jquery.raty.js');
		TPL::import_js('js/jraty/js/jquery.raty.min.js');
		
		//自动完成 jquery.autocomplete
		TPL::import_js('js/jquery.autocomplete.js');
		TPL::import_css('js/jquery.autocomplete.css');
		
		TPL::assign('pagination', AWS_APP::pagination()->create_links());

		TPL::assign('list', $feature_list);
	
		TPL::output("admin/special/sitelist");
	}	
	public function ajax_search_action(){
		$keyword = $_GET['q'];
		$site_str = $this->model('special')->get_search_site($keyword);
		echo $site_str;
	}
	public function edit_action()
	{
		$special = $this->model('special')->get_special_info_by_id(intval($_GET['special_id']),false);
		
		$this->crumb('编辑专辑', "admin/special/list/");
		
		TPL::assign('special', $special);
		
		TPL::output("admin/special/edit");
	}
	public function site_edit_action()
	{
		$qs_info = $this->model('special')->get_qs_info(intval($_GET['qs_id']),false);
	
		$this->crumb('编辑', "admin/special/list/");

		// 评分插件
		TPL::import_js('js/jraty/js/jquery.raty.js');
		TPL::import_js('js/jraty/js/jquery.raty.min.js');
		
		//自动完成 jquery.autocomplete
		TPL::import_js('js/jquery.autocomplete.js');
		TPL::import_css('js/jquery.autocomplete.css');
		
		TPL::assign('qs_info', $qs_info);
	
		TPL::output("admin/special/site_edit");
	}	
	public function topic_list_action()
	{
		$special = $this->model('special')->get_special_info_by_id($_GET['special_id'],false);
		
		$list = $this->model('special')->get_topics_by_special_id($_GET['special_id']);
		
		$this->crumb('管理专题：' . $special['title'] . '  下的标签', "admin/special/list/");
		
		TPL::assign('special', $special);
		
		TPL::assign('list', $list);
		
		TPL::import_js(array(
			'admin/js/jquery.autocomplete.js',
		));
		
		TPL::output("admin/special/topic_list");
	}
	
	public function add_topic_ajax_action()
	{
		$special_id = intval($_GET['special_id']);
		
		$topic_id = intval($_POST['topic_id']);
		
		if(!$special_id)
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, '专题ID为空.'));
		}
		
		if(!$topic_id)
		{
			if(!$topic_info = $this->model('topic')->get_topic_by_title($_POST['topic']))
			{
				H::ajax_json_output(AWS_APP::RSM(null, -1, '标签不存在'));
			}
			
			$topic_id = $topic_info['topic_id'];
		}
		
		if($this->model('special')->add_topic($special_id, $topic_id))
		{
			$this->model('special')->update_topic_count($special_id);
			
			H::ajax_json_output(AWS_APP::RSM(null, 1, null));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, '标签已存在'));
		}
	}
	
	public function delete_topic_ajax_action()
	{
		if($this->model('special')->delete_topic($_GET['special_id'], $_GET['topic_id']))
		{
			$this->model('special')->update_topic_count(intval($_GET['special_id']));
		}
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, null));
	}
	
	public function delete_special_ajax_action()
	{
		if($this->model('special')->del_special_info($_GET['special_id']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, 1, ''));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, '删除失败'));
		}
	}
	public function delete_special_question_ajax_action()	//删除专辑收录的网站
	{
		if($this->model('special')->del_special_question($_GET['qs_id']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, 1, ''));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, -1, '删除失败'));
		}
	}
	public function save_ajax_action()
	{
		define('IN_AJAX', TRUE);
		
		$special_id = intval($_GET['special_id']);
		$title = $_POST['title'];
		if (empty($title))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "专题标题不能为空"));
		}
		
		if($special_id)
		{
			$special = $this->model('special')->get_special_info_by_id($special_id,false);
		}
		
		if($_POST['url_token'])
		{
			if (!preg_match("/^(?!__)[a-zA-Z0-9_]+$/i", $_POST['url_token']))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '专题别名只允许输入英文或数字'));
			}
			
			if (preg_match("/^[\d]+$/i", $_POST['url_token']) AND ($special_id != $_POST['url_token']))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '专题别名不可以全为数字'));
			}
		
			if ($this->model('special')->check_url_token($_POST['url_token'], $special_id))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '专题别名已经被占用请更换一个'));
			}
		}
		//增加新专题
		$data = array(
				'title' => $title,
				'description' => htmlspecialchars($_POST['description']),
				'icon' => $_POST['icon'],
				'css' => htmlspecialchars($_POST['css']),
				'url_token' => $_POST['url_token'],
		);		
		if (! $special_id)
		{		
			$special_id = $this->model('special')->add_special($data);
		/*	if ($_POST['add_nav_menu'])
			{
				$this->model('menu')->add_nav_menu($title, htmlspecialchars($_POST['description']), 'special', $special_id);
			}
		*/	
		}
	
		$update_data = array(
			'title' => $title,
			'description' => htmlspecialchars($_POST['description']),
			'css' => htmlspecialchars($_POST['css']),
			'url_token' => $_POST['url_token'],
		);
	
		if ($_FILES['icon']['name'])
		{
			AWS_APP::upload()->initialize(array(
				'allowed_types' => 'jpg,jpeg,png,gif',
				'upload_path' => get_setting('upload_dir') . '/special',
				'is_image' => TRUE
			))->do_upload('icon');
			
			
			if (AWS_APP::upload()->get_error())
			{
				switch (AWS_APP::upload()->get_error())
				{
					default:
						H::ajax_json_output(AWS_APP::RSM(null, '-1', '错误代码: ' . AWS_APP::upload()->get_error()));
					break;
					
					case 'upload_invalid_filetype':
						H::ajax_json_output(AWS_APP::RSM(null, '-1', '文件类型无效'));
					break;	
				}
			}
			
			if (! $upload_data = AWS_APP::upload()->data())
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '上传失败, 请与管理员联系'));
			}

			foreach (AWS_APP::config()->get('image')->feature_thumbnail as $key => $val)
			{
				$thumb_file[$key] = $upload_data['file_path'] . $special_id . "_" . $val['w'] . "_" . $val['h'] . '.jpg';
				
				AWS_APP::image()->initialize(array(
					'quality' => 90,
					'source_image' => $upload_data['full_path'],
					'new_image' => $thumb_file[$key],
					'width' => $val['w'],
					'height' => $val['h']
				))->resize();	
			}
			
			unlink($upload_data['full_path']);
			
			$update_data['icon'] = basename($thumb_file['min']);
		}
	
		$this->model('special')->update_special($special_id, $update_data);
	
		H::ajax_json_output(AWS_APP::RSM(array(
			'url' => G_INDEX_SCRIPT . 'admin/special/list/'
		), 1, "修改成功"));
	
	}
	public function saveSite_ajax_action()
	{
		define('IN_AJAX', TRUE);
	
		$siteId = intval($_POST['siteId']);
		$comment = $_POST['comment'];
		$score = intval($_POST['score']);
		$special_id =intval($_POST['special_id']);
		$qs_id =intval($_POST['qs_id']);
		$isbook =intval($_POST['isbook']);
		$showtype = ($_POST['showtype']);
		if (empty($siteId))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "请选择网站！"));
		}
		if($isbook && $special_id){
		//	echo $isbook.'-'.$special_id;
			$this->model('special')->update_book($special_id,$isbook);		//更新封面
		}
		if(!$qs_id)
		{
			if (!preg_match("/^(?!__)[a-zA-Z0-9_]+$/i", $score))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '评分错误'));
			}
				
/* 			if (preg_match("/^[\d]+$/i", $_POST['url_token']) AND ($special_id != $_POST['url_token']))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '专题别名不可以全为数字'));
			} */
	
			if ($this->model('special')->check_site_token($siteId, $special_id))
			{
				H::ajax_json_output(AWS_APP::RSM(null, '-1', '本专辑下已经存在该网站'));
			}
		}
		//增加网站
		$data = array(
				'question_id' => $siteId,
				'special_id' => $special_id,
				'comment' => $comment,
				'score' => $score,
		);
		if (!$qs_id)	//判断是添加还是更改
		{
			$qs_id = $this->model('special')->add_special_site($data);
		}
	
		$update_data = array(
				'question_id' => $siteId,
				'special_id' => $special_id,
				'comment' => $comment,
				'score' => $score,
		);	
// 		print_r($update_data);
// 		echo $qs_id;
		//exit($showtype);
		$this->model('special')->update_qs($qs_id, $update_data);
		if($showtype==2){	//跳转类型
			$url='special/explore/'.$special_id;
		}else{
			$url='admin/special/site_list/special_id-'.$special_id;
		}
		H::ajax_json_output(AWS_APP::RSM(array(
				'url' => G_INDEX_SCRIPT . $url
		), 1, "修改成功"));
	
	}	
}